package cn.edu.nuc.creditSystem.service.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.nuc.creditSystem.bean.user.User;
import cn.edu.nuc.creditSystem.dao.BaseMapper;
import cn.edu.nuc.creditSystem.dao.user.inf.UserInfMapper;
import cn.edu.nuc.creditSystem.dao.user.inf.UserMapper;
import cn.edu.nuc.creditSystem.service.BaseImpl;
import cn.edu.nuc.creditSystem.service.user.service.UserService;
@Service
public class UserImpl  extends BaseImpl<User> implements UserService{
	@Autowired
	private UserMapper um;
	@Autowired
	private UserInfMapper uim;
	@Override
	public BaseMapper<User> getBaseDao() {
		return um;
	}
	@Override
	public Boolean ExitsUsername(User user) {
		List<User> list=um.findAll(user);
		if(!list.isEmpty() && list.size()>0){
			return false;
		}else{
			return true;
		}
	}
	@Override
	public User checkUsernameAndPassword(User user) {
		List<User> list=um.findAll(user);
		if(!list.isEmpty() && list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public int ForgetPass(User user) {
		String idcard=user.getIdcard();
		if(idcard!=null && idcard.length()>0){
			user.setPassword(idcard.substring(idcard.length()-6,idcard.length()));
			return um.update(user);
		}else{
			return 0;
		}
	}
	@Override
	@Transactional
	public int insertUser(User model) {
		um.insert(model);
		if(model.getId()!=null && model.getUserinf()!=null){
			model.getUserinf().setId(model.getId());
			return uim.insert(model.getUserinf());
		}
		return -1;
	}
	@Override
	public int updateUser(User model) {
		um.update(model);
		if(model.getId()!=null && model.getUserinf()!=null){
			model.getUserinf().setId(model.getId());
			if(uim.get(model.getId())!=null){
				return uim.update(model.getUserinf());
			}else{
				return uim.insert(model.getUserinf());
			}
		}
		return -1;
	}
}
